package com.bw.paper.mvp.view.user;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.bw.paper.R;
import com.bw.paper.action.BroadAction;
import com.bw.paper.mvp.base.model.BaseEntity;
import com.bw.paper.mvp.base.view.BaseActivity;
import com.bw.paper.mvp.contract.LoginContract;
import com.bw.paper.mvp.di.DaggerLoginComponent;
import com.bw.paper.mvp.di.LoginModules;
import com.bw.paper.mvp.model.HttpCode;
import com.bw.paper.mvp.model.entity.ResponseUserEntity;
import com.bw.paper.mvp.presenter.LoginPresenter;
import com.bw.paper.utils.ToastUtils;

public class RegisterActivity extends BaseActivity<LoginPresenter>implements LoginContract.ILoginView,View.OnClickListener {

    private ImageView head_back_img;
    private EditText login_phone_edt,login_pwd_edt;
    private Button register_btn;

    @Override
    public View addStatusView() {
        return f(R.id.register_linear);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        head_back_img = f(R.id.head_back_img);
        login_phone_edt = f(R.id.login_phone_edt);
        login_pwd_edt = f(R.id.login_pwd_edt);
        register_btn = f(R.id.register_btn);
        head_back_img.setOnClickListener(this::onClick);
        register_btn.setOnClickListener(this::onClick);
    }

    @Override
    public void initData() {

    }

    @Override
    public void inject() {
        DaggerLoginComponent.builder().loginModules(new LoginModules(this))
                .build().injectRegister(this);
    }

    @Override
    public void refreshView(BaseEntity entity) {
        ResponseUserEntity responseUserEntity = (ResponseUserEntity) entity;
        Intent intent = new Intent();
        intent.setAction(BroadAction.UPDATEUSERACTION);
        intent.putExtra("userValues",responseUserEntity.getValues());
        sendBroadcast(intent);
        finish();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.head_back_img == id){
            finish();
        }else if (R.id.register_btn == id){
            if (login_phone_edt.getText()==null){
                ToastUtils.showMsg("手机号为空");
                return;
            }
            if (login_pwd_edt.getText() == null){
                ToastUtils.showMsg("密码为空");
                return;
            }
            p.requestLogin(Long.valueOf(login_phone_edt.getText().toString()),login_pwd_edt.getText().toString(), HttpCode.REGISTCODE);
        }
    }
}
